package com.erlinyou.im.web.config.ws;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import com.erlinyou.im.web.wshandler.ChatWebSocketHandler;
import com.erlinyou.im.web.wshandler.HandshakeInterceptor;
@Configuration
@EnableScheduling
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
	private static Logger logger = LoggerFactory.getLogger(WebSocketConfig.class);
    public WebSocketConfig() {

    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    	
    	
    	//允许连接的域,只能以http或https开头
    			String[] allowsOrigins = {"*"};

    			//WebIM WebSocket通道 创建一个WebSocket配置类（这里也可以用配置文件来实现其实），实现接口来配置Websocket请求的路径和拦截器
    			registry.addHandler(chatWebSocketHandler(),"/socket").setAllowedOrigins(allowsOrigins).
    					addInterceptors(new HandshakeInterceptor());
//    	        registry.addHandler(systemWebSocketHandler(), "/sockjs/webSocketServer").setAllowedOrigins(allowsOrigins).addInterceptors(new WebSocketHandshakeInterceptor())
//    	                .withSockJS();
    }

    @Bean
    public WebSocketHandler chatWebSocketHandler() {
        return new ChatWebSocketHandler();
    }
    
	
	@Bean
	public TaskScheduler taskScheduler(){
	    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
	    taskScheduler.setPoolSize(10);
	    taskScheduler.initialize();
	    return taskScheduler;
	}

}